package com.jiyun.vo.day07;

import com.jiyun.model.day07.Catalog;

import java.util.ArrayList;
import java.util.List;

/**
 * Author yangYingJie
 * Date 2023/8/5  10:58
 */
public class CatalogId {

    public List<Catalog> catalogList=new ArrayList<>();

    public CatalogId(List<Catalog> catalogList){
        this.catalogList=catalogList;
    }

    //所有根节点
    public List<Catalog> getCatalog(){
       List<Catalog> rootCatalogList = new ArrayList<>();
        for (Catalog catalog : catalogList) {
            if(0==catalog.getPid()){
                rootCatalogList.add(catalog);
            }
        }
        return rootCatalogList;
    }
    //根据根节点构造树
    public List<Catalog> buildCatalog(){
        List<Catalog> treeCatalog=new ArrayList<>();
        for (Catalog treeRootCatalog : getCatalog()) {
            treeRootCatalog=buildChildCatalog(treeRootCatalog);
            treeCatalog.add(treeRootCatalog);
        }
        return treeCatalog;
    }

    //创建子树结构
    public Catalog buildChildCatalog(Catalog pNode){
        List<Catalog> childCatalog=new ArrayList<>();
        for (Catalog catalog1 : catalogList) {
            if(catalog1.getPid().toString().equals(pNode.getId().toString())){
                childCatalog.add(buildChildCatalog(catalog1));
            }
        }
        pNode.setChildren(childCatalog);
        return pNode;

    }

}
